package com.lituoxy.dialer;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.huawei.rcs.call.CallApi;
import com.huawei.rcs.call.CallSession;

/**
 * 作者：许航晨 on 2017/4/18 12:11
 * 邮箱：948340983@qq.com
 * 来电界面 待接听界面
 */

public class ACT_DemoCallIncoming extends Activity {

    private CallSession callSession = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_call_incoming);
        /* Get the value of callsession from an Intent. */
        getExtras();
        initViews();
        /* Register a broadcast receiver which is used for detecting call status. */
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(callStatusChangedReceive,
                new IntentFilter(CallApi.EVENT_CALL_STATUS_CHANGED));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        /* Unregister broadcast receiver. */
        LocalBroadcastManager.getInstance(getApplicationContext())
                .unregisterReceiver(callStatusChangedReceive);
    }

    public void onClick_Reject(View v) {
        callSession.terminate();
        stopMP3();
    }

    /**
     * 开始通话
     *
     * @param v
     */
    public void onClick_Answer(View v) {
        callSession.accept(CallSession.TYPE_AUDIO);
        stopMP3();
    }

    private void stopMP3() {
        MediaPlayer mp = ((DialerApplication) this.getApplication()).getMp();
        if (mp != null) {
            mp.stop();
            ((DialerApplication) this.getApplication()).stopMp();
        }
    }

    public void onClick_VideoAnswer(View v) {
        callSession.accept(CallSession.TYPE_VIDEO);
    }

    private void getExtras() {
        long sessionId = getIntent().getLongExtra("session_id", CallSession.INVALID_ID);
        callSession = CallApi.getCallSessionById(sessionId);
    }

    private void initViews() {
        if (null == callSession) {
            finish();
            return;
        }
        boolean isVideoCall = (callSession.getType() == CallSession.TYPE_VIDEO);
        TextView txtContactNumber = (TextView) findViewById(R.id.call_txt_contact_num_incoming);
        TextView txtCallType = (TextView) findViewById(R.id.call_txt_call_state_incoming);
        ViewGroup videoCallAnswerSection = (ViewGroup) findViewById(R.id.call_video_call_answer_section);
        Button btnAnswer = (Button) findViewById(R.id.call_btn_answer);
        /* which widget want to display, it depends on calling type. */
        videoCallAnswerSection.setVisibility(isVideoCall ? View.VISIBLE : View.INVISIBLE);
        btnAnswer.setVisibility(isVideoCall ? View.INVISIBLE : View.VISIBLE);

        txtContactNumber.setText("电话号码: " + callSession.getPeer().getNumber());
        txtCallType.setText(isVideoCall ? "视频电话" : "音频电话");

    }

    /* Deal with call incoming event when call status was changed. */
    private BroadcastReceiver callStatusChangedReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            CallSession session = (CallSession) intent.getSerializableExtra(CallApi.PARAM_CALL_SESSION);
			/* Call session should be checked against. */
            if (!callSession.equals(session)) {
                return;
            }
            int newStatus = intent.getIntExtra(CallApi.PARAM_NEW_STATUS, CallSession.STATUS_IDLE);
            switch (newStatus) {
                case CallSession.STATUS_CONNECTED:
                    Intent intentTalking = new Intent();
				/* Page layout will be changed, according to calling type. */
                    if (session.getType() == CallSession.TYPE_AUDIO) {//音频类型
                        intentTalking.setClass(ACT_DemoCallIncoming.this, ACT_DemoCallTalking.class);
                    } else {//视频类型
                        //intentTalking.setClass(ACT_DemoCallIncoming.this , ACT_DemoCallTalkingVideo.class);
                    }
                    startActivity(intentTalking);
                    ((DialerApplication) getApplication()).isCalling = false;
                    ((DialerApplication) getApplication()).Calling = true;
                    ((DialerApplication) getApplication()).applicationSession = null;
                    finish();
                    break;
                case CallSession.STATUS_IDLE:
                    ((DialerApplication) getApplication()).isCalling = false;
                    ((DialerApplication) getApplication()).Calling = true;
                    ((DialerApplication) getApplication()).applicationSession = null;
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

}